import {Button, Drawer, Form, Input, Select, Space} from "antd";
import {DEF_TYPE_LIST, EDIT_TYPE_MAP} from "@/pages/DataDef/constants";

const layout = {
  labelCol: {span: 8},
  wrapperCol: {span: 16},
};
const tailLayout = {
  wrapperCol: {offset: 8, span: 16},
};

const DefDrawer = ({visible, dispatchers, type, originValues, areaCode, areaList}) => {
  const [form] = Form.useForm();

  const initialValues = {'name': '', 'code': '', 'type':'0', 'acode': areaCode};

  const onFinish = (values: any) => {
    if (type === EDIT_TYPE_MAP.ADD) {
      dispatchers.onAdd(values);
    } else {
      dispatchers.onUpdate({...values, uid: originValues.uid});
    }
  };

  if (visible && type !== EDIT_TYPE_MAP.ADD) {
    form.setFieldsValue(originValues);
  } else if (visible && type === EDIT_TYPE_MAP.ADD) {
    form.setFieldsValue(initialValues);
  }

  return (
    <Drawer title="数据定义" placement="right" onClose={dispatchers.onClose} visible={visible} key={"def-drawer-main"}>
      <Form {...layout} form={form} name="def-drawer" onFinish={onFinish}>
        <Form.Item name="name" label="名称" rules={[{required: true}]}>
          <Input/>
        </Form.Item>
        <Form.Item name="code" label="编码" rules={[{required: true}]}>
          <Input disabled={type !== EDIT_TYPE_MAP.ADD}/>
        </Form.Item>
        <Form.Item name="type" label="类型">
          <Select style={{width: 120}} disabled={type !== EDIT_TYPE_MAP.ADD && originValues.auditFlag > 2}>
            {DEF_TYPE_LIST.map((item) => (
              <Select.Option value={item.defKey} key={item.defKey} disabled={item.defKey === "3"}>{item.defName}</Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item name="acode" label="数据域" rules={[{required: true}]}>
          <Select style={{width: 120}} disabled={type !== EDIT_TYPE_MAP.ADD && originValues.auditFlag > 2}>
            {areaList.map((item) => (
              <Select.Option value={item.code} key={item.code} >{item.name}</Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item {...tailLayout}>
          <Space>
            <Button type="primary" htmlType="submit">
              保存
            </Button>
            <Button type="default" onClick={dispatchers.onClose}>
              取消
            </Button>
          </Space>
        </Form.Item>
      </Form>
    </Drawer>
  );
}

export default DefDrawer;
